import { useCallback, useRef } from 'react'

export function useThrottle(callback: (...args: any[]) => void, delay: number) {
	const lastCall = useRef(0)

	return useCallback(
		(...args: any[]) => {
			const now = new Date().getTime()
			if (now - lastCall.current < delay) return

			lastCall.current = now
			return callback(...args)
		},
		[callback, delay]
	)
}

export function useDebounce(callback: (...args: any[]) => void, delay: number) {
	const timer = useRef<NodeJS.Timeout>()

	return useCallback(
		(...args: any[]) => {
			if (timer.current) clearTimeout(timer.current)

			timer.current = setTimeout(() => {
				callback(...args)
			}, delay)
		},
		[callback, delay]
	)
}
